﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TileEditorApplication
{ 
    public partial class ObjectImport : Form
    {
        public Bitmap preview = null;

        public string fileName;
        public string objectName;

        public string propFilePath;

        public int objectWidth;
        public int objectHeight;

        public ObjectImport()
        {
            InitializeComponent();

            CheckFilePath();
        }

        private void splitContainer1_Panel1_Paint(object sender, PaintEventArgs e)
        {
            if (preview != null)
            {
                Rectangle source = new Rectangle(0, 0, preview.Width, preview.Height);

                Rectangle dest = new Rectangle(0, 0, (int)(preview.Width * (ObjImportSplit.Panel1.Height/(float)preview.Height)), (int)(preview.Height * (ObjImportSplit.Panel1.Height/(float)preview.Height)));

                e.Graphics.DrawImage(preview, dest, source, GraphicsUnit.Pixel);
            }
        }

        private void ObjectBrowseButton_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "png|*.png|jpeg|*.jpg|bmp|*.bmp";
            dlg.FilterIndex = 1;

            if (DialogResult.OK == dlg.ShowDialog())
            {
                if (dlg.FileName != propFilePath + "Graphics\\Props\\" + dlg.SafeFileName)
                {
                    MessageBox.Show(Form1.ActiveForm, "Unable to load image. Please load images from\nthe proper resource folder:\n" + propFilePath + "\nGraphics\\Props\\");
                    return;
                }

                fileName = "Graphics\\Props\\" + dlg.SafeFileName;
                preview = new Bitmap(dlg.FileName);

                ObjectWidthUpDown.Value = preview.Width;
                ObjectHeightUpDown.Value = preview.Height;
                ObjImportSplit.Panel1.Invalidate();
                ObjectImportButton.Enabled = true;
            }
        }

        private void ObjectImportButton_Click(object sender, EventArgs e)
        {
            if (preview != null)
            {                

                objectWidth = (int)ObjectWidthUpDown.Value;
                objectHeight = (int)ObjectHeightUpDown.Value;

                objectName = ObjectNameBox.Text;

                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        public void CheckFilePath()
        {
            try
            {
                System.IO.StreamReader reader = new System.IO.StreamReader(Application.StartupPath + "\\data\\resource_path.txt");

                propFilePath = reader.ReadLine();

                reader.Close();
            }
            catch
            {
                FolderBrowserDialog dlg = new FolderBrowserDialog();
                dlg.Description = "Find the resources folder";

                if (DialogResult.OK == dlg.ShowDialog(this))
                {
                    propFilePath = dlg.SelectedPath + "\\";
                }
            }
        }

        private void ObjectImport_FormClosing(object sender, FormClosingEventArgs e)
        {
            System.IO.StreamWriter writer = new System.IO.StreamWriter(Application.StartupPath + "\\data\\resource_path.txt");
            writer.WriteLine(propFilePath);

            writer.Close();
        }
    }
}
